#include "stdafx.h"
#include "windows.h"
#include "application.h"
#include "ListenServer.h"
#include "Client.h"
#include "FileDB.h"

Application::Application(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(StartServer()));
	connect(ui.connect_btn,SIGNAL(clicked()),this,SLOT(ConnectServer()));
	connect(ui.test_btn,SIGNAL(clicked()),this,SLOT(Test()));

	pListenServer = new ListenServer(this);
	pClient = new Client(this);

	if(AllocConsole()) {
		freopen("CONOUT$", "wt", stdout);
	}
}

Application::~Application()
{

}

void Application::StartServer()
{
	if (pListenServer)
	{
		pListenServer->Start();
		QString port = pListenServer->GetPort();
		ui.port_textEdit->setPlainText(port);
		ui.ip_address_textEdit->setPlainText("127.0.0.1");
	}
	//QMessageBox::about(NULL, "About", "About this application");
}

void Application::ConnectServer()
{
	if (pClient)
	{
		pClient->Start();
		pClient->Connect(ui.ip_address_textEdit->toPlainText(),
						ui.port_textEdit->toPlainText());
	}
}
